package com.module.TraceSource.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.module.TraceSource.vo.TsCodeForRecord;

import base.dao.GenericHibernateDao;
import base.util.Page;


public class TsCodeForRecordDao extends GenericHibernateDao<TsCodeForRecord,Long>{
	private static  TsCodeForRecordDao dao;
	private TsCodeForRecordDao(){
		
	}
	public synchronized static TsCodeForRecordDao getInstance(){
		if(dao == null){
			dao = new TsCodeForRecordDao();
		}
		return dao;
	}
	
	
	public Page getList(Session session,TsCodeForRecord bean,Integer start, Integer limit)throws SQLException{
		Page page = new Page();
		StringBuffer sb = new StringBuffer();
	    StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		
		if(bean.getName()!=null&&!"".equals(bean.getName())){
			condition.append(" and obj.name like ?");
			params.add("%"+bean.getName() + "%");
		}
		if(bean.getCode()!=null&&!"".equals(bean.getCode())){
			condition.append(" and obj.code like ?");
			params.add("%"+bean.getCode() + "%");
		}
		if(bean.getOrgId()!=null&&!"".equals(bean.getOrgId())){
			condition.append(" and obj.orgId like ?");
			params.add("%"+bean.getOrgId() + "%");
		}
		sb.append("from TsCodeForRecord as obj where 1=1 and obj.state='000001' ");
		sb.append(condition);
		sb.append(" order by id desc");
	
		Query query = session.createQuery(sb.toString());
	        int i = 0;
			for (Object obj : params) {
				query.setParameter(i, obj);
				i++;
			}
		query.setFirstResult(start).setMaxResults(limit);
		
		Query totalQuery = session
		.createQuery("select count(*) from TsCodeForRecord as obj where 1=1 and  obj.state='000001' "+condition);
		int j = 0;
		for (Object obj : params) {
			totalQuery.setParameter(j, obj);
			j++;
		}
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());

		//为了显示类别
		java.util.List list = query.list();
		TsKindDao kinddao =TsKindDao.getInstance();
		List<Object> listTs = new ArrayList<Object>();
		boolean typeSee = false;//是否有类型查询
		for(int k=0;k<list.size();k++){
			TsCodeForRecord tsCodeForRecord = (TsCodeForRecord)list.get(k);
			Long kindid=tsCodeForRecord.getKindId();
			if(kindid!=null){
				tsCodeForRecord.setKindName(kinddao.get(session,kindid).getName());
			}
			if(bean.getKindName()!=null&&!"".equals(bean.getKindName())){//将类型符合的记录加入listTs
				typeSee = true;
				if(tsCodeForRecord.getKindName().indexOf(bean.getKindName())!=-1){
					listTs.add(list.get(k));					
				}
			}
		}
		if(typeSee){
			list = listTs;
		}
		//end
		page.setRows(list);	
		return page;
	}
	/**
	 * 
	 * @param session
	 * @param kindId
	 * @return
	 * @throws Exception
	 */
	public boolean isExist(Session session,Long kindId)throws Exception{
		//Query query=session.createQuery();
		SQLQuery query=session.createSQLQuery(" select count(*) from ts_code_for_record where i_kind_id="+kindId);
		int n=((java.math.BigInteger)query.uniqueResult()).intValue();
		return n>0?true:false;
	}
	
	/**
	 * 判断码是否已经被使用了
	 * @param session
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public boolean codeByUsedJudge(Session session,String code)throws Exception{
		SQLQuery query=session.createSQLQuery(" select count(*)  from ts_code_for_record where c_code='"+code+"'");
		int n=((java.math.BigInteger)query.uniqueResult()).intValue();
		return n>0?true:false;
	}  
	
	public boolean isExistRecord(Session session,String name,String orgId)throws Exception{
		SQLQuery query = session.createSQLQuery(" select count(*)  from ts_code_for_record where c_name='"+name+"' and c_org_id='"+orgId+"'");
		int n=((java.math.BigInteger)query.uniqueResult()).intValue();
		return n>0?true:false;
	}
	
	/**
	 * 根据码获得对应的信息
	 * @param session
	 * @param code
	 * @return
	 */
	public TsCodeForRecord findTsCodeForRecordByCode(Session session,String code){
		Query query=session.createQuery(" from TsCodeForRecord as a where a.code='"+code+"'");
		return (TsCodeForRecord)query.uniqueResult();
	}
	
}
